/**
 * Created by Administrator on 15-7-15.
 */
/**
 * 材料
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
    'use strict';
    module.exports = function (app) {
        app.register.controller('TempPlanAddCtrl', ['$scope', '$routeParams', 'MHService',
            function ($scope, $routeParams, MHService) {
                $scope.showSubmit = ($routeParams['flag'] === 'add');
                /**
                 * 监听输入框
                 */
                $scope.$watch('search', function () {
                    //name变化时
                    if ($scope.search && $scope.search.length > 0) {
                        MHService.postReq({url: MHService.URL.fees_search + '?key=' + $scope.search}, function (data) {
                            $scope.feeds = data['datas'];
                        });
                    }
                });
                /**
                 * 选中的材料
                 * @param feed
                 */
                $scope.feedSlected = function (feed) {
                    $scope.selected = angular.copy(feed);
                    $scope.feeds = undefined;
                    $scope.search = undefined;
                    //图片
                    if (feed[11]) {
                        $scope.images = feed[11].split(',');
                        console.info($scope.images);
                    }
                };


                $scope.savePlanMode = function (selected) {
                    if ($scope.num && selected[6]) {
                        //  selected.push({num: $scope.num, price: selected[6]});//数量
                        //var submit = [selected[0], selected.num, selected.time].join(','),//最终提交的数据
                        //  showData = {id: selected[0], name: selected[1], time: selected[7]};//展示在材料列表的数据
                        //提交模块
                        /*account	材料数量
                         gardenid	项目编号
                         processid	工序编号
                         materialid   材料编号
                         price    价格
                         procedureid 流程编号
                         */
                        var gardenid = MHService.globalData(false, {key: 'projectId'}),//项目id
                            processid = MHService.globalData(false, {key: 'processId'}),//工序id
                            modeid=MHService.globalData(false, {key: 'modeid'}),//模块id
                            procedureid = MHService.globalData(false, {key: 'procedureid'});//流程id
                        var req = {
                            account: $scope.num,
                            gardenid: gardenid,
                            processid:processid,
                            materialid:selected[0],
                            price:selected[6],
                            procedureid:procedureid,
                            istemp:1,
                            modeid:modeid
                        };
                        MHService.postReq({url: MHService.URL.save_tempPlan, _req: req}, function (data) {
                            if (data['success']) {
                                //成功
                                MHService.Toast('提交成功');
                                history.go(-1);
                            }
                        });

                        history.go(-1);
                    } else {
                        MHService.Toast('请检查单价和数量');
                    }
                };
            }]);
    };
});
